define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'article/article/index' + location.search,
                    add_url: 'article/article/add',
                    edit_url: 'article/article/edit',
                    del_url: 'article/article/del',
                    multi_url: 'article/article/multi',
                    import_url: 'article/article/import',
                    table: 'article',
                }
            });
			Table.config.dragsortfield = 'sort'

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
	                    {field: 'title', title: __('Title'), operate: 'LIKE'},
	                    {field: 'cid', title: __('Cid'), table: table, formatter: function(value,row){return row.category.title}},
                        // {field: 'author', title: __('Author'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image_input', title: __('Image_input'), formatter: Table.api.formatter.image},
	                    {field: 'recom', title:__('推荐'), formatter: Table.api.formatter.label},
	                    {field: 'visit', title: __('Visit')},
	                    {field: 'sort', title: __('Sort')},
                        {field: 'is_show', title: __('Is_show'), searchList: {"1":__('Normal'),"0":__('Hidden')}, align: 'center', table: table, formatter: Table.api.formatter.toggle},
	                    {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
	                        buttons: [
		                        {
	                                name: 'recom-web',
	                                text: __('PC端'),
	                                classname: 'btn btn-dialog',
			                        dropdown: '推荐',
			                        url: 'article.article/recom/type/web',
		                        },
		                        {
	                                name: 'recom-wap',
	                                text: __('移动端'),
	                                classname: 'btn btn-dialog',
			                        dropdown: '推荐',
			                        url: 'article.article/recom/type/wap',
		                        }
	                        ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'article/article/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'article/article/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'article/article/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
	    recom: function () {
			Controller.api.bindevent();
			$('#delete').click(function(){
				const type = $(this).data('type');
				layer.confirm('确定要取消推荐吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function(index, layero){
					layer.close(index);
					Controller.api.recomCancel(Config.row.id, type);
                }, function(index){

                });
			})
	    },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                require.config({
                    paths: {
                        'tagsinput': '../addons/facrm/js/jquery.tagsinput',
                        'autocomplete': '../addons/facrm/js/jquery.autocomplete',
                    },
                    shim: {
                        'autocomplete': {
                            deps: ['jquery'],
                            exports: '$.fn.extend'
                        },
                        'tagsinput': {
                            deps: ['jquery', 'autocomplete', 'css!../addons/facrm/css/jquery.tagsinput.min.css'],
                            exports: '$.fn.extend'
                        }
                    }
                });
                require(['tagsinput'], function () {
                    //标签输入
                    var elem = "#c-label";
                    var tags = $(elem);
                    tags.tagsInput({
                        width: 'auto',
                        defaultText: '输入后空格确认',
                        minInputWidth: 110,
                        height: '36px',
                        placeholderColor: '#999',
                        onChange: function (row) {
                            if (typeof callback === 'function') {

                            } else {
                                $(elem + "_addTag").focus();
                                $(elem + "_tag").trigger("blur.autocomplete").focus();
                            }
                        },
                        autocomplete: {
                            url: 'article/article/tags',
                            minChars: 1,
                            menuClass: 'autocomplete-tags'
                        }
                    });
                });
            },
	        recomCancel: function(id, type){
				Fast.api.ajax({
					type: 'POST',
					url: 'article/article/recom_cancel',
					data: {id: id, type: type},
					dataType: 'json',
				},function(ret){
					 //提示及关闭当前窗口
                    var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
                    parent.Toastr.success(msg);
                    parent.$(".btn-refresh").trigger("click");
                    if (window.name) {
                        var index = parent.Layer.getFrameIndex(window.name);
                        parent.Layer.close(index);
                    }
                    return false;
				})
	        }
        }
    };
    return Controller;
});
